﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chinaer.IDao;
using Chinaer.Common.Extensions;

namespace Chinaer.Dao.EF
{
    public class CityDAO : Repository<Model.Entities.Model_City>, ICityDAO
    {
        public List<Model.Entities.Model_City> GetCityByProvinceID(string provinceID)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                return context.Cities.Where(l => l.ProvinceID == provinceID && l.IsDelete == false && l.IsUse == true).ToList();
            }
        }

        public override bool Add(Model.Entities.Model_City entity)
        {
            entity.IsModelBaseNullThrowException();
            return base.Add(entity);
        }
        public override bool Delete(Model.Entities.Model_City entity)
        {
            entity.IsModelBaseNullThrowException();
            return base.Delete(entity);
        }
        public override bool Delete(string id)
        {
            Model.Entities.Model_City model_City = GetByID(id);
            return Delete(model_City);
        }
        public override int DeleteByIDs(IList<string> listID)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                var queryResult = from c in context.Cities
                                  where listID.Contains(c.ID)
                                  select c;
                lock (queryResult)
                {
                    foreach (var item in queryResult)
                    {
                        context.Entry<Model.Entities.Model_City>(item).State = System.Data.Entity.EntityState.Modified;
                        item.IsDelete = true;
                    }
                }
                return context.SaveChanges();
            }
        }
        public override bool Exists(string id)
        {
            return GetByID(id) == null ? false : true;
        }
        public override Model.Entities.Model_City GetByID(string id)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                return context.Cities.FirstOrDefault(c => c.ID == id && c.IsUse == true && c.IsDelete == false);
            }
        }
        public override IList<Model.Entities.Model_City> Query(string sql, int pageIndex, out int recordCount, int pageSize, params System.Data.SqlClient.SqlParameter[] sqlParams)
        {
            return base.Query(sql, pageIndex, out recordCount, pageSize, sqlParams);
        }
        public override bool Update(Model.Entities.Model_City entity)
        {
            entity.IsModelBaseNullThrowException();
            return base.Update(entity);
        }



        public Model.Entities.Model_City GetByName(string cityName)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                return context.Cities.FirstOrDefault(c => c.IsUse == true && c.IsDelete == false && c.CityName.Equals(cityName, StringComparison.InvariantCultureIgnoreCase));
            }
        }

        public Model.Entities.Model_City GetByCode(string cityCode)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                return context.Cities.FirstOrDefault(c => c.IsUse == true && c.IsDelete == false && c.CityCode.Equals(cityCode, StringComparison.InvariantCultureIgnoreCase));
            }
        }
    }
}
